﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_Admin_QuanLyNguoiDung : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            layDanhSachLoaiNguoiDung();

            layDanhSachPhong();

            layDanhSachChucVu();
            
            //layDanhSachNguoiDung();
        }

        layDanhSachNguoiDung();
    }

    protected void layDanhSachLoaiNguoiDung()
    {
        LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();
        lndBUS.themItemVaoDropDownList(drplstMaLoaiNguoiDung);
    }
    protected void layDanhSachPhong()
    {
        PhongBUS pBUS = new PhongBUS();
        pBUS.themItemVaoDropDownList(drplstMaPhong);
    }
    protected void layDanhSachChucVu()
    {
        ChucVuBUS cvBUS = new ChucVuBUS();
        cvBUS.themItemVaoDropDownList(drplstMaChucVu);
    }
    protected void layDanhSachNguoiDung()
    {
        NguoiDungBUS ndBUS = new NguoiDungBUS();
        gvNguoiDung.DataSource = ndBUS.layDanhSachNguoiDung();
        gvNguoiDung.DataBind();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        MyTextBox myTextBox = new MyTextBox();

        MyDropDownList myDropDownList = new MyDropDownList();

        NguoiDung ndDTO = new NguoiDung();
        ndDTO.MaNguoiDung = txtMaNguoiDung.Text.ToLower();
        ndDTO.TenNguoiDung = txtTenNguoiDung.Text.ToUpper();
        ndDTO.MaLoaiNguoiDung = drplstMaLoaiNguoiDung.SelectedValue.ToString();
        ndDTO.MaPhong = drplstMaPhong.SelectedValue.ToString();
        ndDTO.MaChucVu = drplstMaChucVu.SelectedValue.ToString();
        ndDTO.MatKhau = myTextBox.mahoaSHA1(txtMatKhau);
        ndDTO.DienThoaiDiDong = txtDienThoaiDiDong.Text;
        ndDTO.DienThoaiNhaRieng = txtDienThoaiNhaRieng.Text;
        ndDTO.Email = txtEmail.Text.ToLower();
        ndDTO.BiXoa = Convert.ToByte(drplstBiXoa.SelectedValue.ToString());
        
        //Kiem tra rong
        if (myTextBox.kiemtraRong(txtMaNguoiDung) == true)
        {
            Alert.Show("Ma nguoi dung khong duoc rong");
            return;
        }

        if (myTextBox.kiemtraRong(txtTenNguoiDung) == true)
        {
            Alert.Show("Ten nguoi dung khong duoc rong");
            return;
        }

        if (myDropDownList.kiemtraRong(drplstMaLoaiNguoiDung) == true)
        {
            Alert.Show("Ma loai nguoi dung khong duoc rong");
            return;
        }

        if (myDropDownList.kiemtraRong(drplstMaPhong) == true)
        {
            Alert.Show("Ma phong khong duoc rong");
            return;
        }

        if (myDropDownList.kiemtraRong(drplstMaChucVu) == true)
        {
            Alert.Show("Ma chuc vu khong duoc rong");
            return;
        }

        if (myTextBox.kiemtraRong(txtMatKhau) == true)
        {
            Alert.Show("Mat khau khong duoc rong");
            return;
        }

        if (myTextBox.kiemtraRong(txtNhapLaiMatKhau) == true)
        {
            Alert.Show("Mat khau nhap lai khong duoc rong");
            return;
        }

        if (myTextBox.mahoaSHA1(txtNhapLaiMatKhau) != myTextBox.mahoaSHA1(txtMatKhau))
        {
            Alert.Show("Mat khau nhap lai khong chinh xac");
            return;
        }

        if (myTextBox.kiemtraRong(txtDienThoaiDiDong) == true)
        {
            Alert.Show("Dien thoai di dong khong duoc rong");
            return;
        }

        if (myTextBox.kiemtraRong(txtDienThoaiNhaRieng) == true)
        {
            Alert.Show("Dien thoai nha rieng khong duoc rong");
            return;
        }

        if (myTextBox.kiemtraRong(txtEmail) == true)
        {
            Alert.Show("Email khong duoc rong");
            return;
        }

        if (myDropDownList.kiemtraRong(drplstBiXoa) == true)
        {
            Alert.Show("Bi xoa khong duoc rong");
            return;
        }

        //Them
        NguoiDungBUS ndBUS = new NguoiDungBUS();

        if (ndBUS.kiemtraTonTaiMaNguoiDung(ndDTO.MaNguoiDung.ToLower().ToString()) == false)
        {
            //if (ndBUS.kiemtraTonTaiTenNguoiDung(ndDTO.TenNguoiDung.ToUpper().ToString()) == false)
            //{
                if (ndBUS.kiemtraTonTaiEmail(ndDTO.Email.ToLower().ToString()) == false)
                {
                    ndBUS.themNguoiDung(ndDTO);

                    ndBUS.layDanhSachNguoiDung();

                    //Xoa TextBox
                    myTextBox.Xoa(txtMaNguoiDung);
                    myTextBox.Xoa(txtTenNguoiDung);
                    myTextBox.Xoa(txtMatKhau);
                    myTextBox.Xoa(txtNhapLaiMatKhau);
                    myTextBox.Xoa(txtDienThoaiDiDong);
                    myTextBox.Xoa(txtDienThoaiNhaRieng);
                    myTextBox.Xoa(txtEmail);
                }
                else
                {
                    Alert.Show("Email nay da ton tai");
                    return;
                }
            //}
            //else
            //{
            //    Alert.Show("Ten nguoi dung nay da ton tai");
            //    return;
            //}
        }
        else
        {
            Alert.Show("Ma nguoi dung nay da ton tai");
            return;
        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        NguoiDungBUS ndBUS = new NguoiDungBUS();

        ndBUS.xoaNguoiDung(gvNguoiDung);

        layDanhSachNguoiDung();
    }
    protected void gvNguoiDung_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvNguoiDung.PageIndex = e.NewPageIndex;

        layDanhSachNguoiDung();
    }

    //Tim kiem
    protected void timkiemMaNguoiDung()
    {
        string maNguoiDung = txtTimMaNguoiDung.Text.ToLower();

        NguoiDungBUS ndBUS = new NguoiDungBUS();
        gvNguoiDung.DataSource = ndBUS.timkiemMaNguoiDung(maNguoiDung);
        gvNguoiDung.DataBind();
    }
    protected void timkiemTenNguoiDung()
    {
        string tenNguoiDung = txtTimTenNguoiDung.Text.ToUpper();

        NguoiDungBUS ndBUS = new NguoiDungBUS();
        gvNguoiDung.DataSource = ndBUS.timkiemTenNguoiDung(tenNguoiDung);
        gvNguoiDung.DataBind();
    }
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        if (rdoTimMaNguoiDung.Checked)
        {
            timkiemMaNguoiDung();
        }

        if (rdoTimTenNguoiDung.Checked)
        {
            timkiemTenNguoiDung();
        }
    }
}